// @ts-nocheck
/**
 @Name：alive
 @Author：zhangjie
 @version：1.0.0
 @Deprecated：alive 工具类
 */

export default {
  /**
   *获取列表
   */
  getListByKey(list, key) {
    const result = [];
    if (key && list && list.length > 0) {
      for (let i = 0; i < list.length; i++) {
        const item = list[i];
        if (item && item[key] !== undefined && item[key] !== null && item[key] !== '') {
          result.push(item[key]);
        }
      }
    }
    return result;
  },
  /**
   *解析树级结构
   */
  resolveTreeDefaultData(src, rootIds) {
    return this.resolveTreeData(src, 'id', 'parentId', 'children', rootIds);
  },
  /**
   *解析树级结构
   */
  resolveTreeData(src, kidField, pidField, childrenName, rootIds) {
    const keyId = kidField !== undefined && kidField !== null && kidField !== '' ? kidField : 'id';
    const keyPid = pidField !== undefined && pidField !== null && pidField !== '' ? pidField : 'parentId';
    const keyChild = childrenName !== undefined && childrenName !== null && childrenName !== '' ? childrenName : 'children';
    const hasRoot = rootIds !== undefined && rootIds !== null && rootIds !== '';
    const xRootIds = ',' + rootIds + ',';
    let data = [];
    try {
      if (typeof src === 'string') {
        // 解析数据
        data = JSON.parse(src);
      } else {
        // 序列化对象
        const str = JSON.stringify(src);
        // 还原数据
        data = JSON.parse(str);
      }
    } catch (e) {
      // 数据格式错误
    }

    // 组装数据
    if (data && data.length > 0) {
      const map = {};
      const result = [];
      for (let i = 0; i < data.length; i++) {
        const item = data[i];
        if (item) {
          const id = item[keyId];
          if (id !== undefined && id !== null) {
            item[keyId] = id + '';
            map[id] = item;
          }
        }
      }
      for (let j = 0; j < data.length; j++) {
        const _item = data[j];
        if (_item) {
          const _pid = _item[keyPid];
          if (_pid !== undefined && _pid !== null && _pid !== '') {
            // 如果设置rootId，那么要更加rootId为顶级ID
            if (hasRoot === true) {
              if (xRootIds.indexOf(',' + _pid + ',') >= 0) {
                result.push(_item);
              } else {
                const parent = map[_pid];
                if (parent) {
                  let children = parent[keyChild];
                  if (!children) children = [];
                  children.push(_item);
                  parent[keyChild] = children;
                }
              }
            } else {
              const parent = map[_pid];
              if (parent) {
                let children = parent[keyChild];
                if (!children) children = [];
                children.push(_item);
                parent[keyChild] = children;
              } else {
                result.push(_item);
              }
            }
          }
        }
      }
      return result;
    }
    return [];
  },
  /**
   *解析树级结构
   */
  resolveLevelTreeData(src, kidField, pidField, childrenName, rootId) {
    const keyId = kidField !== undefined && kidField !== null && kidField !== '' ? kidField : 'id';
    const keyPid = pidField !== undefined && pidField !== null && pidField !== '' ? pidField : 'parentId';
    const keyChild = childrenName !== undefined && childrenName !== null && childrenName !== '' ? childrenName : 'children';
    let data = [];
    try {
      if (typeof src === 'string') {
        // 解析数据
        data = JSON.parse(src);
      } else {
        // 序列化对象
        const str = JSON.stringify(src);
        // 还原数据
        data = JSON.parse(str);
      }
    } catch (e) {
      // 数据格式错误
    }

    // 组装数据
    if (data && data.length > 0) {
      const list = [];
      const rootList = [];
      // 获取到 root data array
      let ids = ',';
      for (let i = 0; i < data.length; i++) {
        const item = data[i];
        if (item) {
          const id = item[keyId];
          const pid = item[keyPid];
          if (id !== undefined && id !== null && id !== '') {
            ids += id + '_' + pid + '_' + i + ',';
          }
        }
      }
      for (let j = 0; j < data.length; j++) {
        const _item = data[j];
        if (_item) {
          const _id = _item[keyId];
          const _pid = _item[keyPid];
          if (_pid !== undefined && _pid !== null && _pid !== '') {
            if ('' + _pid !== '' + _id) {
              if (ids.indexOf(',' + _pid + '_') < 0) {
                if (rootId !== undefined && rootId !== null && rootId !== '') {
                  if ('' + rootId === '' + _pid) {
                    _item['_root'] = true;
                    _item['_tree_parent_id'] = '0';
                    rootList.push(_item);
                  }
                } else {
                  _item['_root'] = true;
                  _item['_tree_parent_id'] = '0';
                  rootList.push(_item);
                }
              }
            }
          }
        }
      }
      // 获取到 root child data array
      if (rootList.length > 0) {
        const recursion = function (data, item, ids, keyId, keyChild) {
          const id = item[keyId];
          const level = item['_tree_level'];
          const parentIds = item['_tree_parent_ids'];
          if (id !== undefined && id !== null && id !== '') {
            // var result = ids.match(/(?<=,)\d*_1_\d*?(?=,)/g);
            // var results = ids.match(new RegExp('(?<=,)\\d*_' + id + '_\\d*?(?=,)', "g"));
            // var result = ids.match(new RegExp('(?<=,\\d*_' + id + '_)\\d*?(?=,)', 'g'));IE 不支持此写法
            const result = ids.match(new RegExp('_' + id + '_\\d*?(?=,)', 'g'));
            if (result && result.length > 0) {
              item['_tree_has_child'] = true;
              for (let j = 0; j < result.length; j++) {
                const info = result[j];
                const lastIndex = info.lastIndexOf('_');
                const indexStr = info.substring(lastIndex + 1);
                const index = parseInt(indexStr);
                const _item = data[index];
                if (_item) {
                  _item['_tree_level'] = level + 1;
                  _item['_tree_parent_ids'] = parentIds + ',' + id;
                  // _item['_tree_parent'] = item
                  // 添加child
                  let child = item[keyChild];
                  if (!child) child = [];
                  child.push(_item);
                  item[keyChild] = child;
                  recursion(data, _item, ids, keyId, keyChild);
                }
              }
            }
          }
        };
        for (let k = 0; k < rootList.length; k++) {
          const it = rootList[k];
          if (it) {
            it['_tree_level'] = 1;
            it['_tree_parent_ids'] = it[keyPid];
            list.push(it);
            recursion(data, it, ids, keyId, keyChild);
          }
        }
        data = list;
      }
    }
    return data;
  },
  /**
   * 去除树形结构中自己及以下的数据
   */
  removeTreeNode(data, id) {
    if (data && data.length > 0) {
      for (let i = data.length - 1; i >= 0; i--) {
        const item = data[i];
        if (item) {
          if (item.id !== undefined &&
            item.id !== null &&
            id !== undefined &&
            id !== null &&
            item.id === id) {
            data.splice(i, 1);
          }
          this.removeTreeNode(item['children'], id);
        }
      }
    }
  },
  /**
   * 判断是否是全屏状态
   */
  isFullScreen() {
    // @ts-ignore
    return document.isFullScreen ||
      document.mozIsFullScreen ||
      document.webkitIsFullScreen;
  },
  /**
   * 全屏切换
   */
  toggleFullScreen() {
    if (this.isFullScreen()) {
      this.exitFullScreen();
    } else {
      this.setFullScreen();
    }
  },
  /**
   * 浏览器全屏
   */
  setFullScreen() {
    // @ts-ignore
    if (document.documentElement.requestFullScreen) {
      // @ts-ignore
      document.documentElement.requestFullScreen();
      return;
    }
    // @ts-ignore
    if (document.documentElement.webkitRequestFullScreen) {
      // @ts-ignore
      document.documentElement.webkitRequestFullScreen();
      return;
    }
    // @ts-ignore
    if (document.documentElement.mozRequestFullScreen) {
      // @ts-ignore
      document.documentElement.mozRequestFullScreen();
      return;
    }
  },
  /**
   * 浏览器退出全屏
   */
  exitFullScreen() {
    // @ts-ignore
    if (document.documentElement.requestFullScreen) {
      // @ts-ignore
      document.exitFullScreen();
      return;
    }
    // @ts-ignore
    if (document.documentElement.webkitRequestFullScreen) {
      // @ts-ignore
      document.webkitCancelFullScreen();
      return;
    }
    // @ts-ignore
    if (document.documentElement.mozRequestFullScreen) {
      // @ts-ignore
      document.mozCancelFullScreen();
      return;
    }
  },
  /**
   * 扩展参数
   */
  extend() {
    const length = arguments.length;
    let target = arguments[0] || {};
    if (typeof target !== 'object' && typeof target !== 'function') {
      target = {};
    }
    if (length === 1) {
      target = this;
    }
    for (let i = 1; i < length; i++) {
      const source = arguments[i];
      for (let key in source) {
        // 使用for in会遍历数组所有的可枚举属性，包括原型。
        if (Object.prototype.hasOwnProperty.call(source, key)) {
          const val = source[key];
          if (typeof val === 'string') {
            target[key] = source[key];
          } else if (typeof val === 'boolean') {
            target[key] = source[key];
          } else if (typeof val === 'number') {
            target[key] = source[key];
          } else if (Object.prototype.toString.call(val) === '[object Array]') {
            target[key] = source[key];
          } else if (typeof val === 'object') {
            target[key] = this.extend({}, val);
          }
        }
      }
    }
    return target;
  },
  /**
   * 深度克隆
   * @param data
   */
  deepClone(data) {
    let obj;
    if (data instanceof Array) {
      obj = [];
      for (let i = 0, len = data.length; i < len; i++) {
        obj.push(this.deepClone(data[i]));
      }
    } else if (typeof data === 'object') {
      obj = {};
      for (let key in data) {
        if (Object.prototype.hasOwnProperty.call(data, key)) {
          obj[key] = this.deepClone(data[key]);
        }
      }
    } else {
      obj = data;
    }
    return obj;
  },
  /**
   * 将对象内的属性转换为Sting，主要是映射字典关系
   */
  string() {
    const length = arguments.length;
    let data = arguments[0];
    if (data) {
      if (data instanceof Array) {
        const list = data;
        for (let i = 0, len = list.length; i < len; i++) {
          const object = list[i];
          if (object) {
            for (let i = 1; i < length; i++) {
              const key = arguments[i];
              if (Object.prototype.hasOwnProperty.call(object, key)) {
                const value = object[key];
                if (value !== undefined && value !== null && value !== '') {
                  object[key] = value.toString();
                }
              }
            }
          }
        }
      } else if (typeof data === 'object') {
        const object = data;
        if (object) {
          for (let i = 1; i < length; i++) {
            const key = arguments[i];
            if (Object.prototype.hasOwnProperty.call(object, key)) {
              const value = object[key];
              if (value !== undefined && value !== null && value !== '') {
                object[key] = value.toString();
              }
            }
          }
        }
      }
    }


  },
  /**
   * 获取uuid
   * @returns {string}
   */
  getUuid() {
    return 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
      const r = Math.random() * 16 | 0;
      const v = c === 'x' ? r : (r & 0x3 | 0x8);
      return v.toString(16);
    });
  }
};
